package com.rybbaby.tss.front.tssClassesLessons.service.imp;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.google.common.collect.ImmutableMap;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClassChangeRecord.entity.TssClassChangeRecord;
import com.rybbaby.tss.front.tssClassChangeRecord.service.ITssClassChangeRecordService;
import com.rybbaby.tss.front.tssClassLessonsExchangeRecord.entity.TssClassLessonsExchangeRecord;
import com.rybbaby.tss.front.tssClassLessonsExchangeRecord.service.ITssClassLessonsExchangeRecordService;
import com.rybbaby.tss.front.tssClasses.dao.ITssClassesDao;
import com.rybbaby.tss.front.tssClasses.entity.TssClasses;
import com.rybbaby.tss.front.tssClassesLessons.dao.ITssClassesLessonsDao;
import com.rybbaby.tss.front.tssClassesLessons.entity.TssClassesLessons;
import com.rybbaby.tss.front.tssClassesLessons.service.ITssClassesLessonsService;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import net.sf.json.JSONSerializer;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 班级课节对调表数据库操作服务实现
 * @createDate 2017-08-09 下午11:50:14
 */
@Service
@Transactional
public class TssClassesLessonsServiceImp extends BaseServiceImp<TssClassesLessons> implements ITssClassesLessonsService {

    @Autowired
    private ITssClassesLessonsDao dao;
    @Autowired
    private ITssClassesDao classesDao;
    @Autowired
    private ITssClassLessonsExchangeRecordService tssClassLessonsExchangeRecordService;
    @Autowired
    private ITssClassChangeRecordService tssClassChangeRecordService;
    @Autowired
    private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;


    /**
     * 根据班级Id和亲子学生id查询剩余课时数
     *
     * @return
     */
    public Integer getSurplusCurriculum(String classId, String memberId){
       return this.dao.getSurplusCurriculum(classId,memberId);
    }


    @Override
    public List<Map<String, Object>> getListByClassId(String classId) {
        return dao.getListByClassId(classId);
    }

    @Override
    public Map<String, Object> findByClassIdCreateTime(String classId, Timestamp classTime) {
        return dao.findByClassIdCreateTime(classId, classTime);
    }

    @Override
    public Integer countCouldChangeLessons(String id) {
        return dao.countCouldChangeLessons(id);
    }

    @Override
    public Integer updateParameterById(Map<String, Object> map, String id) {
        return dao.updateParameterById(map, id);
    }


    @Override
    public Map<String, Object> classLessonsExchangeByClassTime(String classTime, Integer classType) {
        return dao.classLessonsExchangeByClassTime(classTime, classType);
    }

    @Override
    public Boolean updateLessonClassTime(String lessonsOne, String classTimeOne, String lessonsTwo, String classTimeTwo, String adminUserId) throws Exception {
        boolean bool = true;
        List<TssClassLessonsExchangeRecord> lessonsExchangeRecordList = new ArrayList<>();
        List<TssClassChangeRecord> classChangeRecordList = new ArrayList<>();
        //修改课节信息
        String lessonsOnes[] = StringUtils.split(lessonsOne, ",");
        String lessonsTwos[] = StringUtils.split(lessonsTwo, ",");
        for (String lesson : lessonsOnes) {
            String beforeJson = JSONSerializer.toJSON(mapJson(lesson)).toString();
            this.dao.updateParameterById(ImmutableMap.of("classTime", classTimeTwo), lesson);
            String afterJson = JSONSerializer.toJSON(mapJson(lesson)).toString();
            addChangeRecord(classChangeRecordList, lesson, afterJson, beforeJson);
            addChangeFieldRecord(lessonsExchangeRecordList, adminUserId, lesson, classTimeTwo + ":" + lessonsTwo, classTimeOne + ":" + lessonsOne);
        }
        for (String lesson : lessonsTwos) {
            String beforeJson = JSONSerializer.toJSON(mapJson(lesson)).toString();
            this.dao.updateParameterById(ImmutableMap.of("classTime", classTimeOne), lesson);
            String afterJson = JSONSerializer.toJSON(mapJson(lesson)).toString();
            addChangeRecord(classChangeRecordList, lesson, afterJson, beforeJson);
            addChangeFieldRecord(lessonsExchangeRecordList, adminUserId, lesson, classTimeOne + ":" + lessonsOne, classTimeTwo + ":" + lessonsTwo);
        }

        this.tssClassChangeRecordService.jdbcBatchSave(classChangeRecordList);
        this.tssClassLessonsExchangeRecordService.jdbcBatchSave(lessonsExchangeRecordList);
        return bool;
    }

    /**
     * 添加班级变动信息
     *
     * @param lesson
     */
    private Map<String, Object> mapJson(String lesson) {
        Map<String, Object> map = new HashedMap();
        //查询班级课节变更之前信息
        TssClassesLessons tartgetLessons = this.get(lesson);
        map.put("lessonInfo", tartgetLessons);
        //查看班级变更之前信息
        TssClasses beforeTargerClass = this.classesDao.get(TssClasses.class, tartgetLessons.getId());
        map.put("classInfo", beforeTargerClass);
        return map;
    }


    /**
     * 添加班级变动记录
     *
     * @param classChangeRecordList
     */
    private void addChangeRecord(List<TssClassChangeRecord> classChangeRecordList, String lesson, String afterJson, String beforeObject) {
        TssClassChangeRecord changeRecord = new TssClassChangeRecord();
        changeRecord.setBeforeJsonInfo(beforeObject);
        changeRecord.setAfterJsonInfo(afterJson);
        classChangeRecordList.add(changeRecord);
    }

    /**
     * 添加班级对调记录
     *
     * @param lessonsExchangeRecordList
     * @param adminUserId
     * @param afterValue
     * @param beforeValue
     */
    private void addChangeFieldRecord(List<TssClassLessonsExchangeRecord> lessonsExchangeRecordList, String adminUserId, String lessonId, String afterValue, String beforeValue) {
        TssClassLessonsExchangeRecord lessonsExchangeRecord = new TssClassLessonsExchangeRecord();
        lessonsExchangeRecord.setCreateTime(new Timestamp(System.currentTimeMillis()));
        lessonsExchangeRecord.setAdminUserId(adminUserId);
        lessonsExchangeRecord.setLessonId(lessonId);
        lessonsExchangeRecord.setAfterValue(afterValue);
        lessonsExchangeRecord.setBeforeValue(beforeValue);
        lessonsExchangeRecordList.add(lessonsExchangeRecord);
    }


    @Override
    public Boolean stopClassByLessonIds(String ids) {
        Boolean bool = true;
        //对课节进行停课操作
        String lessonsId[] = ids.split(",");
        for (String lessonId : lessonsId) {
            this.updateParameterById(ImmutableMap.of("status", 2), lessonId);
            //已报班会员报班记录状态为停课状态
            String sql = "UPDATE tss_member_attend_class_record SET STATUS = 3 WHERE  lessonsId =?";
            Integer rowCount = this.tssMemberAttendClassRecordService.executeSql(sql, new Object[]{lessonId});
            if (rowCount == 0) {
                return !bool;
            }
        }
        return bool;
    }

    @Override
    public List<Map<String, Object>> queryListByClassId(String classId) {
        return dao.queryListByClassId(classId);
    }

    @Override
    public List<Map<String, Object>> queryByClassTimeAndAdminUserId(String empAdminUserId, String classTime, Integer type) {
        return dao.queryByClassTimeAndAdminUserId(empAdminUserId,classTime,type);
    }

}
